define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                var store_id = $('#store_id').val();
                var cart_total;
                getCart();
                $(document).on('click','.checkbox',function(){
                    var check_total = $('.item .checkbox').length;
                    cart_total = 0;

                    if($(this).hasClass('checkall')){
                        if($(this).hasClass('icon-quan')){
                            $('.checkbox').removeClass('icon-quan').addClass('icon-xuanzhong');
                        }else{
                            $('.checkbox').addClass('icon-quan').removeClass('icon-xuanzhong');
                        }
                    }else{
                        var check_num = 0;
                        $(this).toggleClass('icon-quan').toggleClass('icon-xuanzhong');
                        $('.item .icon-xuanzhong').each(function(){
                            check_num ++;
                            if(check_num < check_total){
                                $('.checkall').addClass('icon-quan').removeClass('icon-xuanzhong');
                            }else{
                                $('.checkall').removeClass('icon-quan').addClass('icon-xuanzhong');
                            }
                        })
                    }
                    $('.item .icon-xuanzhong').each(function(){
                        var goods_num = $(this).data('goods_num');
                        var goods_price = $(this).data('goods_price');
                        cart_total = cart_total + Number(goods_num * goods_price);
                    })
                    $('#cart_total').html(cart_total.toFixed(2));
                })

                $('#delete').click(function(){
                    var goods_arr = [];
                    $('.icon-xuanzhong').each(function(){
                        goods_arr.push($(this).data('goods_id'));
                    })
                    if(goods_arr.length == 0){
                        fm.alert('请先选择商品','background:#fff;color:#2a2a2a');
                    }else{
                        fm.confirm('<p class="text-16 confirm_t">删除提示</p><p>你确定要删除这些商品么？</p>', function (callback) {
                            if(callback == 1){
                                deleteCart(goods_arr);
                            }
                        }, 'border-top: 4px solid #00a5f3;min-width: 64%;');
                    }

                })

                var check_num = true;
                $(document).on('click', '.change-num-btn', function () {
                    check_num = false;
                    goods_id = $(this).data('goods_id');
                    var status = $(this).data('status');
                    var total = $('.tips').html();
                    var num = $(this).siblings('.cart-num').html();
                    var single_price = $(this).data('price');

                    cart_total = Number($('#cart_total').html());

                    if (status == 'plus') {
                        if (cart('plus')) {
                            num ++;
                            total ++;
                            cart_total += Number(single_price);
                            check_num = true;
                        }
                    } else {
                        if (cart('sub')) {
                            num --;
                            total --;
                            cart_total -= single_price;
                            if (num == 0) {
                                $('.item-' + goods_id).remove();
                            }
                            check_num = true;
                        }
                    }
                    if(total > 0){
                        $('.tips').html(total).removeClass('hide');
                    }else{
                        $('.tips').html(total).addClass('hide');
                    }
                    $('.cart-num-' + goods_id).html(num);
                    $('#cart_total').html(cart_total.toFixed(2));
                    $('.checkbox-' + goods_id).attr('data-goods_num',num);
                })

                $(document).on('click','#doOrder',function(){
                    var send_storage = [];
                    $('.item .icon-xuanzhong').each(function(){
                        send_storage.push({goods_id:$(this).data('goods_id'),goods_num:$(this).data('goods_num'),goods_price:$(this).data('goods_price'),goods_name:$(this).data('goods_name'),goods_thumb:$(this).data('goods_thumb'),cart_id:$(this).data('cart_id')});
                    })
                    if(send_storage.length == 0){
                        fm.alert('请先选择商品','background:#fff;color:#2a2a2a');
                    }else{
                        console.log(send_storage);
                        $.ajax({
                            url: '/restaurant/TrustworthyShop/sendMark',
                            type: 'post',
                            data: {send_storage:send_storage,store_id:store_id},
                            dataType: 'json',
                            success: function (data) {
                                if(data.status == 'S'){
                                    location.href = '/restaurant/TrustworthyShop/sendshow/store_id/' + store_id;
                                }else{
                                    fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                }
                            }
                        })
                    }

                })

                function getCart(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/cartShow',
                        type: 'post',
                        data: {store_id: store_id},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.no_more').remove();
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status = 'S'){
                                var html = '',no_more = '';
                                if(data.cartCount > 0){
                                    $('.tips').html(data.cartCount).removeClass('hide');
                                }

                                if(data.cartData && data.cartData.length > 0){
                                    $.each(data.cartData,function(i,k){
                                        html += '<div class="item list-item gc-flex gc-flex-lc item-' + k.goods_id + '">' +
                                            '<div class="iconfont icon-quan gc-ft-gray checkbox checkbox-' + k.goods_id + '" data-goods_id="' + k.goods_id + '" data-goods_num="'+ k.goods_num +'" data-goods_price="' + k.goods_price + '"data-goods_name="' + k.goods_name + '"data-goods_thumb="' + k.goods_thumb + '"data-cart_id="' + k.cart_id + '"></div>' +
                                            '<div class="item-img"><img src="' + k.goods_thumb + '"></div><div class="item-msg gc-flex-item">' +
                                            '<div class="item-name gc-ft-normal">' + k.goods_name + '</div>' +
                                            '<div class="gc-flex gc-flex-lc"><span class="gc-ft-red price">￥' + k.goods_price + '</span><span class="gc-ft-sm gc-ft-gray">已售' + k.sale_num + '</span>' +
                                            '<div class="quantity gc-flex-item gc-ft-right">' +
                                            '<a href="javascript:;" class="iconfont icon-jian gc-ft-deepgray change-num-btn" data-goods_id="' + k.goods_id + '" data-status="sub" data-price="' + k.goods_price + '"></a>' +
                                            '<span class="gc-ft-deepgray cart-num cart-num-' + k.goods_id + '">'+ k.goods_num +'</span>' +
                                            '<a href="javascript:;" class="iconfont icon-jia gc-ft-deepgray change-num-btn" data-goods_id="' + k.goods_id + '" data-status="plus" data-price="' + k.goods_price + '"></a></div></div></div></div>';
                                    })
                                }else{
                                    html = '<div class="full"><div class="empty"><img src="/assets/restaurant/TrustworthyShop/images/empty.png"/></div>' +
                                        '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">购物车空空如也~</p></div>';
                                }
                                $('#carts').html(html);
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

                function cart(type) {
                    var option_flag;
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/addCarts',
                        type: 'post',
                        async: false,
                        data: {store_id: store_id,goods_id:goods_id,type:type},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                option_flag = true;
                                // fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }else{
                                option_flag = false;
                                // fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                    return option_flag;
                }

                /*删除购物车记录*/
                function deleteCart(goods_arr){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/delCarts',
                        type: 'post',
                        data: {store_id: store_id,goods_id:goods_arr},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                setTimeout(function(){
                                    location.reload();
                                },500)
                            }else{
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }
                            $('.circle-loadding').hide();
                        }
                    })

                }

            });
        });
    };
});
